package com.hns.can.master.service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hns.can.common.aop.orgTree.LeftOrgAnnotation;
import com.hns.can.common.constants.GlobalConst;
import com.hns.can.common.entity.BaseEntity;
import com.hns.can.common.formula.FillCodeDscValue;
import com.hns.can.common.formula.FillParamsVo;
import com.hns.can.master.entity.BaseCarDrvRelEntity;
import com.hns.can.master.entity.BaseCarInfoEntity;
import com.hns.can.master.entity.BaseLineCarRelEntity;
import com.hns.can.master.vo.LineCarRelParamsVo;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.frame.service.CommonService;
import com.hns.tool.pub.PubUtil;
import com.hns.tool.pub.UtilString;

/**
 * 线路车辆分配的service
 * @author:zhangyu
 * @email:zhangyu2222z@sina.cn
 * @version Revision 2.0.0
 */
@Service
public class LineCarRelService extends CommonService {
	/**
	 * 填充代码描述
	 */
    @Autowired
    private FillCodeDscValue codeDscValue;
    
    /**
     * 获取线路车辆关联
     * @param entity 查询信息
     * @param corpId 公司id
     * @return 线路车辆关联信息
     * @throws QueryExprException
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @SuppressWarnings({"rawtypes", "unchecked" })
    @LeftOrgAnnotation()
    @Transactional
    public Pagination getLineCarRel(List<String> orgIds,LineCarRelParamsVo entity) throws Exception{
        Pagination page = this.myBatisPage("lineCarRel.getLineCarRelCount", "lineCarRel.getLineCarRelData", entity,  BaseLineCarRelEntity.class);
        List list = page.getData();
        Map<String,String> map = new HashMap<String,String>();
        map.put("licPltNo", GlobalConst.DEFAULT_CACHE_VALUE);  
        map.put("carType", "carType");
        map.put("operCertiNo","operCertiNo"); 
        map.put("remark","remark"); 
        FillParamsVo vo = new FillParamsVo("carId", map);
//        codeDscValue.fillInfoValue(list, vo, GlobalConst.CACHE_KEY_BASE_CAR_INFO);
        map = new HashMap<String,String>();
        map.put("lineName", GlobalConst.DEFAULT_CACHE_VALUE);   
        FillParamsVo vo5 = new FillParamsVo("lineId", map);
        codeDscValue.fillInfoValue(list, new String[]{GlobalConst.CACHE_KEY_BASE_CAR_INFO, GlobalConst.CACHE_KEY_BASE_LINE_INFO}, vo, vo5);
        page.setData(list);
        return page;
    }
    
    /**
     * 添加线路车辆关联
     * @param userId 当前用户
     * @param lineId 线路id
     * @param carIds 车辆id
     * @param corpIds 公司id
     * @throws Exception
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @Transactional
    public void saveLineCarRel(String userId,String lineId, String[] carIds, String[] corpIds) throws Exception{
        for(int i=0;i<carIds.length;i++){
            if(getCarFromRel(carIds[i])>0){
                throw new BusinessException(MsgType.ERROR.getFlag(), "该车辆已经添加到其他线路中!");
            }
            BaseLineCarRelEntity entity = new BaseLineCarRelEntity();
            entity.setRelId(UtilString.getUUID());
            entity.setCarId(carIds[i]);
            entity.setLineId(lineId);
            entity.setCorpId(corpIds[i]);
            entity.setCrtUser(userId);
            Date date = new Date();
            entity.setCrtTime(new Timestamp(date.getTime()));
            commonEntityDao.save(entity);
        }
    }

    /**
     * 获取未与任何线路关联的车辆
     * @param entity 线路车辆信息
     * @param corpId 公司id
     * @return 未绑定车辆信息
     * @throws QueryExprException
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    public Pagination getUnboundCar(BaseLineCarRelEntity entity,String corpId) throws Exception{
        entity.setCorpId(corpId);
        Pagination page = this.myBatisPage("lineCarRel.getUnboundCarCount", "lineCarRel.getUnboundCarData", entity,  BaseLineCarRelEntity.class);
        List<Object> list = page.getData();
        Map<String,String> map = new HashMap<String,String>();
        map.put("organName", GlobalConst.DEFAULT_CACHE_VALUE);   
        FillParamsVo vo2 = new FillParamsVo("organId", map);
        codeDscValue.fillInfoValue(list, new String[]{GlobalConst.CACHE_KEY_BASE_ORGAN}, vo2);
        page.setData(list);
        return page;
    }

    /**
     * 删除线路车辆关系
     * @param relId 线路车辆关联id
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @Transactional
    public void delLineCarRel(String relId) {
        commonEntityDao.delete(BaseLineCarRelEntity.class, new String[]{relId});
    }

    /**
     * 获取全部符合条件线路车辆关系
     * @param corpId 公司id
     * @param entity 查询条件
     * @return 线路车辆绑定信息
     * @throws QueryExprException
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @SuppressWarnings({"unchecked" })
    @LeftOrgAnnotation()
    @Transactional
    public List<BaseEntity> getAllLineCarRel(List<String> orgIds,LineCarRelParamsVo entity) throws Exception{
        List<BaseEntity> list =   PubUtil.convertListMapToObject(this.sqlMapDao.selectList("lineCarRel.getLineCarRelData", entity), BaseCarInfoEntity.class);
        Map<String,String> map = new HashMap<String,String>();
        map.put("licPltNo", GlobalConst.DEFAULT_CACHE_VALUE);  
        map.put("operCertiNo","operCertiNo"); 
        map.put("carType", "carType");
        map.put("remark","remark"); 
        FillParamsVo vo = new FillParamsVo("carId", map);
        map = new HashMap<String,String>();
        map.put("lineName", GlobalConst.DEFAULT_CACHE_VALUE);   
        FillParamsVo vo5 = new FillParamsVo("lineId", map);
        codeDscValue.fillInfoValue(list, new String[]{GlobalConst.CACHE_KEY_BASE_CAR_INFO, GlobalConst.CACHE_KEY_BASE_LINE_INFO}, vo, vo5);
        Map<String,String> mapTmp = new HashMap<String,String>();
        mapTmp.put("carType", "CAT00000");
        mapTmp.put("binding", "BIN00000");
        codeDscValue.fillCodeDscValue(list, mapTmp);
        return list;
    }
    
    /**
     * 校验是否存在已存储的车辆
     * @param carId 车辆id
     * @return 已存在车辆数目
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @SuppressWarnings({"rawtypes", "unchecked" })
    private int getCarFromRel(String carId){
        Map param = new HashMap();
        param.put("carId", carId);
        List list = commonEntityDao.findByParamsIgnoreNullValue(BaseLineCarRelEntity.class, param);
        return list.size();
    }

    /**
     * 获取人车绑定状态
     * @param carId 车辆id
     * @return 车辆绑定司机数目
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @SuppressWarnings({"rawtypes", "unchecked" })
    public int getCarDriverBinding(String carId) {
        Map param = new HashMap();
        param.put("carId", carId);
        List list = commonEntityDao.findByParamsIgnoreNullValue(BaseCarDrvRelEntity.class, param);
        return list.size();
    }
    
}
